package com.suse.boss.controller;

import com.suse.boss.backInfo.BackInfo;
import com.suse.boss.backInfo.BackInfoMap;
import com.suse.boss.backInfo.BackInfos;
import com.suse.boss.entity.ChatMsgs;
import com.suse.boss.entity.User;
import com.suse.boss.entity.UserHeader;
import com.suse.boss.mapper.ChatMsgsMapper;
import com.suse.boss.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserMapper userMapper;
    @Autowired
    ChatMsgsMapper chatMsgsMapper;

    @GetMapping("/user/{id}")
    public User getuser(@PathVariable("id" ) int id){
        return userMapper.findOneById( id );
    }

    @PostMapping("/register")
    public BackInfo register(@RequestBody User user, HttpServletResponse response){
        if(userMapper.findOne( user.getUsername())!=null)
        return new BackInfo(1,"用户已存在");
        userMapper.saveUser( user );
        user.setPassword( null );
        Cookie cookie=new Cookie( "userid",user.get_id().toString() );
        response.addCookie( cookie );
        return new BackInfo( 0,user);
    }

    @PostMapping("/login")
    public BackInfo login(@RequestBody User users,HttpServletResponse response){
        User user=userMapper.login( users.getUsername(), users.getPassword());
        if(user!=null) {
            Cookie cookie=new Cookie( "userid",user.get_id().toString() );
            cookie.setPath("/");
            response.addCookie( cookie );
            return new BackInfo( 0, user );
        }
        else
            return new BackInfo(1,"用户名或密码错误");
    }

    @PostMapping("/update")
    public BackInfo update(@RequestBody User user, @CookieValue("userid") String userid, HttpServletResponse response) {
        System.out.println( userid );
        if (userid == null)
            return new BackInfo( 1, "请先登录" );
        User backUser = userMapper.findOneById( Integer.parseInt( userid ) );
        if (backUser == null){
            Cookie cookie=new Cookie( "userid",null );
            cookie.setPath("/");
            response.addCookie( cookie );
            return new BackInfo( 1, "请先登录" );
        }
        else{
            userMapper.updateUser( user , Long.parseLong( userid ));
            backUser.setPassword( null );
            backUser.setCompany( user.getCompany());
            backUser.setHeader( user.getHeader() );
            backUser.setInfo( user.getInfo() );
            backUser.setPost( user.getPost() );
            backUser.setSalary( user.getSalary() );
            return new BackInfo( 0, backUser);
        }
    }

    @GetMapping("/user")
    public BackInfo user( @CookieValue("userid") String userid, HttpServletResponse response) {
        if (userid == null)
            return new BackInfo( 1, "请先登录" );
        User backUser = userMapper.findOneById( Integer.parseInt( userid ) );
        if (backUser == null){
            Cookie cookie=new Cookie( "userid",null );
            cookie.setPath("/");
            response.addCookie( cookie );
            return new BackInfo( 1, "请先登录" );
        }
        else{
            backUser.setPassword( null );
            return new BackInfo( 0, backUser);
        }
    }
    @GetMapping("/userlist")
    public BackInfos userList( String type){
        List<User> userList= userMapper.listByType( type );
        return new BackInfos( 0,userList );
    }

    @GetMapping("/msglist")
    public BackInfoMap msglist(@CookieValue("userid") String userid){
        List users = userMapper.listAllHeader();
        List chatMsgs = chatMsgsMapper.listmsgs( Long.parseLong(userid) );
        Map chatmsg = new HashMap<String,UserHeader>(  );
        for(int i=0;i<users.size();i++){
            User user=(User)users.get(i);
            chatmsg.put( user.get_id(),new UserHeader(user.getUsername(),user.getHeader()) );
        }
        Map result = new HashMap<String,Object>(  );
        result.put( "users",chatmsg );
        result.put("chatMsgs",chatMsgs);
        return new BackInfoMap( 0 ,result );
    }

    @PostMapping("/readmsg")
    public BackInfo redmsg(@RequestBody ChatMsgs chatMsgs,@CookieValue("userid") String userid){
        int re = chatMsgsMapper.readFalse( chatMsgs.getFrom(),Long.parseLong(userid)  );
        chatMsgsMapper.readMsgs( chatMsgs.getFrom(),Long.parseLong(userid) );
        return  new BackInfo( 0,re );
    }
}
